package busi

import (
	"fmt"
	"gorm.io/gorm"
	"math/rand"
	"time"
)

var (
	db       *gorm.DB
	allAreas = []Area{}
	ra       *rand.Rand
)

func PrevLoad(gdb *gorm.DB) {
	db = gdb
	if err := db.Table("area").Scan(&allAreas).Error; err != nil {
		fmt.Println(err)
		return
	}
	ra = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func ScheduleWork(jobName string, param ...interface{}) {
	switch jobName {
	case "hisim":
		tasks := []string{"out"} //"orderIn", "orderUpdate", "orderUpdate","patient", "admit", "out","transfer"
		idxTask := ra.Intn(1000) % len(tasks)
		switch tasks[idxTask] {
		case "orderIn":
			fmt.Println("==> orderIn")
			OrderIn()
			break
		case "orderUpdate":
			fmt.Println("==> orderUpdate")
			OrderUpdate()
			break
		case "patient":
			fmt.Println("==> patient")
			PatientCome()
			break
		case "transfer":
			fmt.Println("==> transfer")
			PatientTransfer()
			break
		case "admit":
			fmt.Println("==> admit")
			AdmitPatient()
			break
		case "out":
			fmt.Println("==> out")
			PatientOut()
			break
		case "ifs":
			fmt.Println("==> ifs")
			StartIfs()
			break
		}
	}
}
